import fs from '@ohos.file.fs';
import utils from '../core/utils';
import logger from '../core/logger';
import convertXml from '@ohos.convertxml';
import danmaku, { xmlItem } from './danmaku';
import { danmakuSize } from '../enums/danmakuSize';
import { danmakuType } from '../enums/danmakuType';
import { danmakuPoolType } from '../enums/danmakuPoolType';

class danmakuParser {
    loadXmlFromFile(path: string): Promise<Array<danmaku>> {
        return new Promise((resolve) => {
            let arr: Array<danmaku> = [];
            try {
                fs.readText(path).then(async (str) => {
                    arr = await this.loadXmlFromString(str);
                }).catch((err) => logger.error(err));
            } catch (e) {
                logger.error(e);
            }
            resolve(arr);
        });
    }

    loadXmlFromString(xml: string): Promise<Array<danmaku>> {
        return new Promise((resolve) => {
            let arr: Array<danmaku> = [];
            try {
                let conv = new convertXml.ConvertXML();
                let options = {
                    trim: true,
                    declarationKey: "_declaration",
                    instructionKey: "_instruction",
                    attributesKey: "_attributes",
                    textKey: "_text",
                    cdataKey: "_cdata",
                    doctypeKey: "_doctype",
                    commentKey: "_comment",
                    parentKey: "_parent",
                    typeKey: "_type",
                    nameKey: "_name",
                    elementsKey: "_elements"
                }
                let result: any = conv.convertToJSObject(xml, options);
                if (!utils.isEmpty(result)) {
                    let root: Array<xmlItem> = result._elements[0]?._elements;
                    if (!utils.isEmpty(root) && root.length > 0) {
                        root = root.filter((i) => i._name == "d");
                        for (let row of root) {
                            let d = new danmaku();
                            d.text = row._elements[0]?._text;
                            let attr = row._attributes?.p?.split(',');
                            if (!utils.isEmpty(attr)) {
                                d.time = parseFloat(attr[0]) * 1000;
                                d.danmakuType = parseInt(attr[1]) as danmakuType;
                                d.textSize = parseInt(attr[2]) as danmakuSize;
                                let color = ((0x00000000ff000000 | parseFloat(attr[3])) & 0x00000000ffffffff);
                                d.textColor = color;
                                d.sendTime = parseInt(attr[4]);
                                d.poolType = parseInt(attr[5]) as danmakuPoolType;
                                d.senderUIDHash = attr[6];
                                d.id = parseInt(attr[7]);
                            }
                            arr.push(d);
                        }
                    }
                }
            } catch (e) {
                logger.error(e);
            }
            resolve(arr);
        });
    }
}
export default new danmakuParser();